//表格行
let columns = [[
    {field: 'netDiskType', title: '网盘类型', align:'center', width:100},
    {field: 'taskNum', title: '任务数', align:'center', width:100}
]];

//下拉列表的数据
let comboboxData = [
    {
        "value":0,
        "text":"所有网盘"
    },
    {
        "value":1,
        "text":"百度网盘"
    },
    {
        "value":2,
        "text":"坚果云"
    },
    {
        "value":3,
        "text":"FTP"
    },
    {
        "value":4,
        "text":"Seafile"
    },
    {
        "value":5,
        "text":"SMB"
    }
]

//数据
let _data;

$(function() {
    //加载完成后异步请求数据
    $.ajax({
        url: "admin_netdiskTaskTypeInfo",
        type:'post',
        dataType:'json',
        success:function(value){
            //console.log(JSON.stringify(value));
            if(value.success) {
                _data = value.data;
                //显示数据表格
                showDataGrid(0);
                //显示图表
                showChart(0);
            } else {
                $.messager.alert('提示',value.data, "info");
            }
        }
    });

    //数据表格初始化
    $('#grid').datagrid({
        columns:columns,
        singleSelect:true,
        fitColumns:true
    });

    //初始化下拉列表
    $("#dataSource").combobox({
        data: comboboxData,
        valueField:'value',
        textField:'text',
        onSelect: function (record) {
            showDataGrid(record.value, record.text);
            showChart(record.value, record.text);
        }
    })
});

//初始化数据
function showDataGrid(index, text) {
    //console.log(_data);
    if(index == 0) {
        $("#grid").datagrid("loadData", formatAllData());
    } else {
        $("#grid").datagrid("loadData", formatOneData(--index, text));
    }
}

//初始化图表
function showChart(index, text) {
    if(index == 0) {
        showBarChart();
    } else {
        showPieChart(--index, text);
    }
}

//格式化所有数据
function formatAllData() {
    let chartData = {};
    let rows = [];
    rows.push({
        "netDiskType":"百度网盘",
        "taskNum":_data.downloadData[0] + _data.uploadData[0]
    });
    rows.push({
        "netDiskType":"坚果云",
        "taskNum":_data.downloadData[1] + _data.uploadData[1]
    });
    rows.push({
        "netDiskType":"FTP",
        "taskNum":_data.downloadData[2] + _data.uploadData[2]
    });rows.push({
        "netDiskType":"Seafile",
        "taskNum":_data.downloadData[3] + _data.uploadData[3]
    });
    rows.push({
        "netDiskType":"SMB",
        "taskNum":_data.downloadData[4] + _data.uploadData[4]
    });
    chartData.total = rows.length;
    chartData.rows = rows;
    return chartData;
}

//格式化一个网盘的数据
function formatOneData(index, text) {
    let chartData = {};
    let rows = [];
    rows.push({
        "netDiskType":text,
        "taskNum":_data.downloadData[index] + _data.uploadData[index]
    });
    chartData.total = rows.length;
    chartData.rows = rows;
    return chartData;
}

//显示柱状图
function showBarChart() {
    var chart = {
        type: 'bar'
    };
    var title = {
        text: '各网盘任务类型统计图'
    };
    var xAxis = {
        categories: ['百度网盘', '坚果云', 'FTP', 'Seafile', 'SMB'],
        title: {
            text: null
        }
    };
    var yAxis = {
        min: 0,
        title: {
            text: '个',
            align: 'high'
        },
        allowDecimals:false
    };
    var tooltip = {
        valueSuffix: ' 个'
    };
    var plotOptions = {
        bar: {
            dataLabels: {
                enabled: true
            }
        }
    };
    var credits = {
        enabled: false
    };

    var series = [
        {
            name: '离线下载',
            data: _data.downloadData
        },
        {
            name: '断点上传',
            data: _data.uploadData
        }
    ];

    var json = {};
    json.chart = chart;
    json.title = title;
    json.tooltip = tooltip;
    json.xAxis = xAxis;
    json.yAxis = yAxis;
    json.series = series;
    json.plotOptions = plotOptions;
    $('#container').highcharts(json);
}

//显示饼状图
function showPieChart(index, text) {
    var chart = {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
    };
    var title = {
        text: text + '任务类型统计图'
    };
    var tooltip = {
        pointFormat: '{series.name}: <b>{point.percentage:.0f}%</b>'
    };
    var plotOptions = {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.0f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    };

    var series= [{
        type: 'pie',
        name: '占比',
        data: [
            ['离线下载', _data.downloadData[index]],
            ['断点上传', _data.uploadData[index]]
        ]
    }];

    var json = {};
    json.chart = chart;
    json.title = title;
    json.tooltip = tooltip;
    json.series = series;
    json.plotOptions = plotOptions;
    $('#container').highcharts(json);
}